package com.informationsystem.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.informationsystem.service.SystemuserManager;
import com.informationsystem.service.VideoManager;
import com.informationsystem.dao.SearchException;
import com.informationsystem.model.Video;
import com.informationsystem.webapp.action.BaseAction;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class VideoAction extends BaseAction implements Preparable {
    private VideoManager videoManager;
    private List videoes;
    private Video video;
    private Long id;
    private String query;
    private Date hourformat;


    public void setVideoManager(VideoManager videoManager) {
        this.videoManager = videoManager;
    }

    public List getVideoes() {
        return videoes;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String videoId = getRequest().getParameter("video.id");
            if (videoId != null && !videoId.equals("")) {
                video = videoManager.get(new Long(videoId));
            }
        }
    }

    public void setQ(String q) {
        this.query = q;
    }

    public String list() {
        try {
            videoes = videoManager.search(query, Video.class);
        } catch (SearchException se) {
            addActionError(se.getMessage());
            videoes = videoManager.getAll();
        }
        return SUCCESS;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Video getVideo() {
        return video;
    }

    public void setVideo(Video video) {
        this.video = video;
    }

    public String delete() {
    	video = videoManager.get(video.getId());
        videoManager.remove(video.getId());
        addLog("delete video id:" +video.getId() + " title: "+ video.getTitle());
        saveMessage(getText("video.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            video = videoManager.get(id);

        } else {
            video = new Video();
            video.setSystemuser(getLoginUser());
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (video.getId() == null);
        if(!isNew)
        	video.setPostDate(generatePostDate());
        else
        	video.setPostDate(new Date());
        video = videoManager.save(video);

        String key = (isNew) ? "video.added" : "video.updated";
        saveMessage(getText(key));

        if(!isNew){
        	addLog("edit video id:" +video.getId() + " title: "+ video.getTitle());
        }
        else{
        	addLog("add video id:" +video.getId() + " title: "+ video.getTitle());
        }
        
        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }

	public SystemuserManager getSystemuserManager() {
		return systemuserManager;
	}

	public void setSystemuserManager(SystemuserManager systemuserManager) {
		this.systemuserManager = systemuserManager;
	}
	
	public List getSystemuserList(){
		return systemuserManager.getAll();
	}
	
	public Date getHourformat() {
		return video.getPostDate();
	}

	public void setHourformat(Date hourformat) {
		this.hourformat = hourformat;
	}
	
	@SuppressWarnings("deprecation")
	public Date generatePostDate(){
		Calendar cal = Calendar.getInstance();
		cal.setTime(video.getPostDate()); 
		cal.set(Calendar.HOUR_OF_DAY, hourformat.getHours());
		cal.set(Calendar.MINUTE,hourformat.getMinutes());

		return cal.getTime();
	}
	
}